/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.mortbay.jetty.pico.pages;

import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.hibernate.annotations.CommitAfter;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.mortbay.jetty.pico.dao.category.CategoryDAO;
import org.mortbay.jetty.pico.entities.Category;
import org.mortbay.jetty.utils.Random;

/**
 *
 * @author godfather
 */
public class SaveCategoryAction {
    
    
    @Inject 
    private CategoryDAO categoryDAO;
    @InjectPage
    private CategoryAction categoryAction;
    
//    @Component
//    private BeanEditForm form;
    @Property
    private Category category;
    private String id;
//    
//    public void onValidateForm(){
//        Category cat = categoryDAO.find(category.getCateName());
//        if(cat != null && cat.getIdCate()!= category.getIdCate()){
//            form.recordError("category with name "+ category.getCateName() + " alrealy exists");
//        }
//    }
//    
    
    public void onActivate(String id){
//        if(id == null){
//            category = new Category(Random.RandCode("cate"));
//        }else{
            category = categoryDAO.find(id);
//        }
        this.id = id;
    }
    public String onPasstivate(){
        return id;
    }
    public Object onSubmit(){
        //categoryDAO.save(category);
        return categoryAction;
    }
    @CommitAfter
    public Object onSuccess(){
        categoryDAO.save(category);
        return categoryAction;
    }
    
    void onPrepareFrom() {
        if(category == null || category.getIdCate().isEmpty() || category.getIdCate().equals("")){
            category = new Category(Random.RandCode("cate"));
        }
    }
    
}
